package projet;

import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.naming.InitialContext;
import javax.naming.NamingException;

import projet.ejb.CompteFacade;
import projet.entity.Compte;
import projet.ejb.CompteFacade;

import com.sun.corba.se.spi.orbutil.fsm.Action;

public class Runner implements ActionListener{

	private static Ident frame_ident = new Ident();
	private static Admin frame_admin;
	private static Invest frame_invest;
	private static InitialContext ctx;
	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					ctx = new InitialContext();
					ctx.addToEnvironment(InitialContext.INITIAL_CONTEXT_FACTORY,"org.jnp.interfaces.NamingContextFactory");
					ctx.addToEnvironment(InitialContext.URL_PKG_PREFIXES,"org.jboss.naming:org.jnp.interfaces");
					ctx.addToEnvironment(InitialContext.PROVIDER_URL,"jnp://localhost:1099");
					
						init_ident();
					
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			
			public void init_ident(){
				frame_ident.setVisible(true);
				
				frame_ident.btn_connexion.addActionListener(new ActionListener() {

					public void actionPerformed(ActionEvent e) {
						//test login
						try {
							frame_ident.show_error_connexion(false);
							boolean ok=false;
							boolean admin=false;
							CompteFacade compteFacade = (CompteFacade) ctx.lookup("CompteFacadeEJB/remote");
							 int id_compte=-1;
							try{
								Compte c = (Compte) compteFacade.authentification(frame_ident.txtf_login.getText(), frame_ident.txtf_password.getText());
								id_compte = c.getId_compte();
								admin = c.isAdmin();
								ok=true;
							}
							catch (Exception e2) {
								ok=false;
							}
							
							
							if(!ok){
								//e.
								frame_ident.show_error_connexion(true);
								//frame_ident.setVisible(false);
								//frame_ident.setEnabled(false);
							}
							else{
								
								if(admin){
									
									frame_admin = new Admin(ctx);
									frame_admin.setVisible(true);
								}
								else{
									System.out.println("idcompe: "+id_compte);
									frame_invest = new Invest(ctx, id_compte);
									frame_invest.setVisible(true);
								}
							}
							
						} catch (NamingException e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}
						
						
					}
				});
			}
			
		});
		
		
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		
	}
	
	

}
